﻿using AnesSystem.Controls;
using AnesSystem.Models;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AnesSystem.ViewModels
{
    public class PacuRecordCtrlViewModel : MultitonBaseViewModel
    {
        #region Variables
        protected Dictionary<int, PacuRecordItemCtrlViewModel> _pacuRecordCtrlViewModels = new Dictionary<int, PacuRecordItemCtrlViewModel>();
        protected ObservableCollection<PacuRecordItemCtrlViewModel> _pacuRecordItems = new ObservableCollection<PacuRecordItemCtrlViewModel>();



        #endregion


        #region Constructor
        public PacuRecordCtrlViewModel()
        {

        }


        #endregion


        #region Properties
        public Dictionary<int, PacuRecordItemCtrlViewModel> PacuRecordCtrlViewModels
        {
            get
            {
                return _pacuRecordCtrlViewModels;
            }

            set
            {
                _pacuRecordCtrlViewModels = value;
            }
        }

        public ObservableCollection<PacuRecordItemCtrlViewModel> PacuRecordItems
        {
            get
            {
                return _pacuRecordItems;
            }

            set
            {
                _pacuRecordItems = value;
            }
        }
        public int CurTotalPage
        {
            get
            {
                return CurPatientLink.Status.PacuPage;
            }

        }
        public PatientLinkModel CurPatientLink
        {
            get
            {
                return DataManager.Instance.CurrentPatientLink;
            }

        }

        #endregion


        #region 依赖属性


        #endregion


        #region Event


        #endregion


        #region PrivateMethods
        protected PatientLinkModel GetPatientLink()
        {
            return DataManager.Instance.CurrentPatientLink == null ? null : DataManager.Instance.CurrentPatientLink;
        }

        protected void LoadData()
        {
            GetAllOperationData();
            Refresh();
            for (int i = 1; i <= CurTotalPage; i++)
            {
                PacuRecordCtrlViewModels[i].Refresh();
            }
        }

        protected void GetAllOperationData()
        {
            InitDrugsCache();
            InitEventDataCache();
            InitVitalSignsDataCache();
        }

        protected void InitVitalSignsDataCache()
        {
            VitalSignDataCache vitalSignDataCache = OperationDataPool.Instance.CurOperationData.VitalSignDataCacheDic[Constants.ISPACU];
            vitalSignDataCache.ClearCache();

            foreach (var item in CurPatientLink.VitalSignShow.PacuVitalSign)
            {
                vitalSignDataCache.AddConfigParam(item);
            }
            vitalSignDataCache.RefreshRespEvent();
        }

        protected void InitEventDataCache()
        {
            EventDataCache eventDataCache = OperationDataPool.Instance.CurOperationData.EventDataCacheDic[Constants.ISPACU];
            eventDataCache.ClearCache();
            eventDataCache.DbSearch();
        }

        protected void InitDrugsCache()
        {
            DrugsDataCache drugsDataCache = OperationDataPool.Instance.CurOperationData.DrugsDataCache;
            drugsDataCache.ClearCache();
            drugsDataCache.DbSearch();
        }
        #endregion


        #region Public/ProtectedMethods
        public override void InitData()
        {
            if (!DataManager.Instance.PatientLinkCache.ContainsKey(CurPatientLink.Id))
            {
                PacuRecordItems.Clear();
                PacuRecordCtrlViewModels.Clear();
                for (int page = 1; page <= CurTotalPage; page++)
                {
                    PacuRecordItemCtrlViewModel viewModel = new PacuRecordItemCtrlViewModel(page);
                    PacuRecordItems.Add(viewModel);
                    PacuRecordCtrlViewModels.Add(page, viewModel);
                }
                LoadData();
            }
            else
            {
                BaseTableControl ctrl = DataManager.Instance.PacuRecordCtrl;
                if (ctrl.IsNewCreate)
                {
                    PacuRecordItems.Clear();
                    PacuRecordCtrlViewModels.Clear();
                    for (int page = 1; page <= CurTotalPage; page++)
                    {
                        PacuRecordItemCtrlViewModel viewModel = new PacuRecordItemCtrlViewModel(page);
                        PacuRecordItems.Add(viewModel);
                        PacuRecordCtrlViewModels.Add(page, viewModel);
                    }
                    ctrl.IsNewCreate = false;
                    LoadData();
                }
            }
        }

        public override void Refresh()
        {
            PacuRealTimeSignsConfigCtrlViewModel.Instance.Refresh();

            PacuRealTimeSignMainShowCtrlViewModel.Instance.Refresh();
        }

        #endregion


        #region EventHandlers


        #endregion
    }
}
